package arrayobjectexercise;

import com.xx.yy.Student;

public class Test3 {
    public static void main(String[] args) {
        //1.创建一个数组来存储游戏装备对象
        GameEquipment[] arr = new GameEquipment[3];
        //2.创建游戏装备对象并添加到数组中
        GameEquipment game1 = new GameEquipment(1, "石剑", 5, 100);
        GameEquipment game2 = new GameEquipment(2, "铁剑", 10, 1000);
        GameEquipment game3 = new GameEquipment(3, "钻石剑", 20, 5000);

        //3.把游戏准备对象添加到数组当中
        arr[0] = game1;
        arr[1] = game2;
        arr[2] = game3;

        int index = getIndex(arr,2);

        if(index >= 0){
            GameEquipment game = arr[index];
            int newAge = game.getAttackPower() + 5;
            game.setAttackPower(newAge);
            printArr(arr);
        }else{
            System.out.println("当前id不存在，修改失败");
        }

    }

    public static  int getIndex(GameEquipment[]arr , int id){
        for (int i = 0; i < arr.length; i++){
            GameEquipment game = arr[i];
            if(game != null){
                int sid = game.getId();
                if(sid == id){
                    return i;
                }
            }
        }
        return -1;
    }

    public  static void printArr(GameEquipment[] arr){
        for (int i = 0;i <arr.length; i++){
            GameEquipment game = arr[i];
            if(game != null){
                System.out.println("id: " + game.getId() +" 名称：" + game.getName() + " 攻击力：" + game.getAttackPower() + " 价格：" + game.getPrice());
            }
        }
    }
}
